package com.aynu.webshop.web.controller;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.aynu.webshop.web.model.Category;
import com.aynu.webshop.web.service.CategoryService;
import com.aynu.webshop.web.service.GoodsService;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
@RequestMapping(value = "/category")
public class CategoryController {
	@Resource
	private CategoryService categoryService;
	@Resource
	private GoodsService goodsService;
	/**
	 * 
	 * @param model
	 * @param request
	 * @return 
	 * 方法说明：获取所有分类信息
	 */
	@RequestMapping(value = "/getAllCategory", method = RequestMethod.GET)
	public ModelAndView getAllCategory(HttpServletRequest request)
			throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
		ModelAndView mv = new ModelAndView();
		List<Category> listParent = categoryService.selectListParent();
		for(Category parent : listParent) {
			List<Category> children = categoryService.selectListChild(parent.getCatId());
			parent.setChild(children);
		}
		request.setAttribute("parents", listParent);
		mv.setViewName("category/categoryDetail");
		return mv;
	}
	/**
	 * 
	 * @param model
	 * @param request
	 * @return 
	 * 方法说明：获取所有一级分类信息
	 */
//	@RequestMapping(value = "/getAllCategoryOne", method = RequestMethod.GET)
//	public ModelAndView getAllCategoryOne(HttpServletRequest request)
//			throws NoSuchMethodException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
//		ModelAndView mv = new ModelAndView();
//		List<Category> listParent = categoryService.selectListParent();
////		for(Category parent : listParent) {
////			List<Category> children = categoryService.selectListChild(parent.getCatId());
////			parent.setChild(children);
////		}
//		request.setAttribute("parents", listParent);
//		mv.setViewName("../../../index");
//		return mv;
//	}
	@RequestMapping(value = "/getAllCategoryOne", method = RequestMethod.POST)
	public String getAllCategoryOne(HttpServletRequest request, HttpServletResponse response ,Integer tid) {
		JSONObject result = new JSONObject();
		List<Category> listParent = categoryService.selectListParent();
		JSONArray json = JSONArray.fromObject(listParent); 
		result.put("json", json);
		response.setCharacterEncoding("UTF-8");  
        try {  
            response.getWriter().write(result.toString());  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
		return null;
	}
	/**
	 * 
	 * @param model
	 * @param request
	 * @return 
	 * 方法说明：获取二级分类
	 */
	@RequestMapping(value = "/getCategoryTwoById", method = RequestMethod.POST)
	public String getCategoryTwoById(HttpServletRequest request, HttpServletResponse response ,Integer cid) {
		JSONObject result = new JSONObject();
		List<Category> listChild = categoryService.selectListChild(cid);
		JSONArray json = JSONArray.fromObject(listChild); 
		result.put("json", json);
		response.setCharacterEncoding("UTF-8");  
		try {  
			response.getWriter().write(result.toString());  
		} catch (IOException e) {  
			e.printStackTrace();  
		}  
		return null;
	}
	
	/**
	 * 
	 * @param model
	 * @param request
	 * @return 
	 * 方法说明：删除分类
	 */
	@RequestMapping(value = "/deleteCategory", method = RequestMethod.GET)
	public ModelAndView deleteCategory(HttpServletRequest request,Integer catid){
		ModelAndView mv = new ModelAndView("redirect:getAllCategory");
		Category category = categoryService.selectById(catid);
		if(category.getCatCid()!=null){
			if(goodsService.selectByCatcid(category.getCatCid())>0){
				//该分类下有商品 不可删除
			    System.out.println("不可删除！");
			}else{
				System.out.println("可删除1");
				categoryService.delete(catid);
			}
		}else{
			System.out.println("可删除2");
			categoryService.delete(catid);
		}
		return mv;
	}
	/**
	 * 
	 * @param model
	 * @param request
	 * @return 
	 * 方法说明：添加分类信息
	 */
	@RequestMapping(value = "/addCategory", method = RequestMethod.POST)
	public ModelAndView addCategory(HttpServletRequest request,Category category ){
		ModelAndView mv = new ModelAndView("redirect:getAllCategory");
		categoryService.insert(category);
		return mv;
	}
	/**
	 * 
	 * @param model
	 * @param request
	 * @return 
	 * 方法说明：修改分类信息
	 */
	@RequestMapping(value = "/editCategory", method = RequestMethod.POST)
	public ModelAndView editCategory(HttpServletRequest request,Category category ){
		ModelAndView mv = new ModelAndView("redirect:getAllCategory");
		System.out.println(category);
		categoryService.update(category);
		return mv;
	}
}
